package com.vibeplayer.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.vibeplayer.app.R;
import com.vibeplayer.app.model.Playlist;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.PlaylistViewHolder> {

    private Context context;
    private List<Playlist> playlists;
    private PlaylistClickListener listener;

    public PlaylistAdapter(Context context, List<Playlist> playlists, PlaylistClickListener listener) {
        this.context = context;
        this.playlists = playlists;
        this.listener = listener;
    }

    @NonNull
    @Override
    public PlaylistViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_playlist, parent, false);
        return new PlaylistViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PlaylistViewHolder holder, int position) {
        Playlist playlist = playlists.get(position);
        holder.txtPlaylistName.setText(playlist.getName());
        holder.txtSongCount.setText(context.getString(R.string.songs) + ": " + playlist.getSongCount());
        
        // 格式化创建日期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String dateCreated = dateFormat.format(new Date(playlist.getDateCreated()));
        holder.txtDateCreated.setText(dateCreated);
    }

    @Override
    public int getItemCount() {
        return playlists.size();
    }

    public class PlaylistViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView txtPlaylistName;
        TextView txtSongCount;
        TextView txtDateCreated;

        public PlaylistViewHolder(@NonNull View itemView) {
            super(itemView);
            txtPlaylistName = itemView.findViewById(R.id.txtPlaylistName);
            txtSongCount = itemView.findViewById(R.id.txtSongCount);
            txtDateCreated = itemView.findViewById(R.id.txtDateCreated);
            
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onPlaylistClick(getAdapterPosition());
            }
        }
    }

    public interface PlaylistClickListener {
        void onPlaylistClick(int position);
    }
} 